package com.westcom.vulb.bean;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.graphics.Bitmap;
import android.widget.ImageView;

public class Card {
    public ArrayList<BasicCardResource> resources = new ArrayList<Card.BasicCardResource>();
    public int goodCount = 0;
    public int commentCount = 0;
    public static final String CardHtmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    public static final String CardStartTag = "<card>";
    public static final String CardEndTag = "</card>";
    public int mCardId;
    public Card(int cardId)
    {
    	this.mCardId = cardId;
    }
    
    public static Card parse(int cardId,String html) {
        Card card = new Card(cardId);
        if (html == null || html.isEmpty()) {
            return card;
        }
        
        Document document = Jsoup.parse(html);
        
        String cardHtml = document.select("card").first().html();
        String tmpHtml = cardHtml;
        while (!tmpHtml.isEmpty())//the parse will stop when there is text as a end
        {
            int resStart = tmpHtml.indexOf("<resource", 0);
            if (resStart >= 0)//find a res
            {
                String textHtml = tmpHtml.substring(0, resStart);//try to find the text befor res
                String text = Jsoup.parse(textHtml).text();
                if (!text.isEmpty())
                {
                    card.resources.add(new TextCardResource(BasicCardResource.TYPE_TEXT, textHtml, text));
                }
                int resEnd = tmpHtml.indexOf("/>", resStart) + "/>".length();
                String resHtml = tmpHtml.substring(resStart, resEnd);
                String id = Jsoup.parse(resHtml).select("resource").first().attr("id");
                if (!id.isEmpty())// parse res
                {
                    card.resources.add(new ImageCardResource(BasicCardResource.TYPE_IMAGE, resHtml,Integer.parseInt(id)));
                }
                tmpHtml = tmpHtml.substring(resEnd);
            } else //not find res,try to parse the rest html as a text
            {
                String textHtml = tmpHtml.substring(0);
                String text = Jsoup.parse(textHtml).text();
                if (!text.isEmpty())
                {
                	card.resources.add(new TextCardResource(BasicCardResource.TYPE_TEXT, textHtml, text));
                }
                tmpHtml = "";//parse end
            }
        }
        return card;
    }
    
    public static class BasicCardResource
    {
    	public static final int TYPE_TEXT = 1;
        public static final int TYPE_IMAGE = 2;
        public int mResType;
        public String mHtml;
        public BasicCardResource(int type,String html)
        {
        	mResType = type;
        	mHtml = html;
        }
    }
    public static class TextCardResource extends BasicCardResource
    {
    	private String mText;
    	public TextCardResource(int type,String html,String text)
    	{
    		super(type, html);
    		mText = text;
    	}
    	public void setText(String new_str)
    	{
    		String tmp = this.mHtml;
    		tmp = this.mHtml.replace(this.mText, new_str);
			this.mHtml = tmp;
			this.mText = new_str;
    	}
    	public String getText()
    	{
    		return this.mText;
    	}
    }
    public static class ImageCardResource extends BasicCardResource
    {
    	private int mResId;
    	public String mFileName;
    	public int mFileSize;
    	public Bitmap mImage;
    	public ImageView mView;
    	public ImageCardResource(int type,String html,int resId)
    	{
    		super(type,html);
    		mResId = resId;
    	}
    	public void setResId(int new_resId)
    	{
    		this.mHtml = this.mHtml.replace(""+mResId,""+new_resId);
    		mResId = new_resId;
    	}
    	public int getResId()
    	{
    		return this.mResId;
    	}
    }
    public ImageCardResource getImageRes(int resId)
    {
    	ImageCardResource icr = null;
    	for(int i = 0;i<resources.size();i++)
    	{
    		BasicCardResource tmp = resources.get(i);
    		if(tmp.mResType == BasicCardResource.TYPE_IMAGE)
    		{
    			ImageCardResource icr_tmp = (ImageCardResource)tmp;
	    		if(icr_tmp.mResId == resId)
	    		{
	    			icr = icr_tmp;
	    			break;
	    		}
    		}
    	}
    	return icr;
    }
    public int getImageResIndex(int resId)
    {
    	int index = -1;
    	for(int i = 0;i<resources.size();i++)
    	{
    		BasicCardResource tmp = resources.get(i);
    		if(tmp.mResType == BasicCardResource.TYPE_IMAGE)
    		{
    			ImageCardResource icr_tmp = (ImageCardResource)tmp;
	    		if(icr_tmp.mResId == resId)
	    		{
	    			index = i;
	    			break;
	    		}
    		}
    	}
    	return index;
    }
    
    public String BuildHtml()
    {
    	String html = "";
    	html = html + CardHtmlHeader + CardStartTag;
    	TextCardResource tcr = null;
    	ImageCardResource icr = null;
    	for(BasicCardResource bcr:resources)
    	{
    		html = html + bcr.mHtml;
    	}
    	html = html+CardEndTag;
    	html = html.replace("br /","br");
    	return html;
    }
    public int[] getResIds()
    {
    	ImageCardResource icr = null;
    	int[] resourceIds = null;
    	List<Integer> resIds = new ArrayList<Integer>();
    	for(BasicCardResource bcr:resources)
    	{
    		if(bcr.mResType == BasicCardResource.TYPE_IMAGE)
    		{
    			icr = (ImageCardResource)bcr;
    			resIds.add(icr.mResId);
    		}
    	}
    	Integer []tmp = resIds.toArray(new Integer[0]);
    	resourceIds = new int[tmp.length];
    	for(int i = 0;i<tmp.length;i++)
    	{
    		resourceIds[i] = tmp[i];
    	}
    	return resourceIds;
    }
}